package LC0234;

public class Solution2 {
    public boolean isPalindrome(ListNode head) {
        // 1 2 3 4 4 3 2 1
        if(head == null || head.next == null) {
            return true;
        }
        ListNode slow = head, fast = head;
        ListNode pre = head, prepre = null;
        // 翻转链表，pre是头，prepare是尾

        // 移动指针，同时翻转链表
        while(fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
            pre.next = prepre;
            prepre = pre;
        }
        // 1 2 3 2 1
        // 解决链表长度的奇偶问题
        if (fast != null) {
            slow = slow.next;
            // 抛弃中间结点，以便比较
        }
        // 比较两个链表的结点的值
        while (pre != null && slow != null) {
            if(pre.val != slow.val) {
                return false;
            }
            pre = pre.next;
            slow = slow.next;
        }
        return true;
    }
}
